package com.caoxw.Android.CityBus;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ConfigActivity extends ActivityEx {
	private Spinner m_objDataList = null;
	private Spinner m_objTimeOut = null;
	private Spinner m_objMaxDeep = null;
	private Button m_objMoreData = null;
	private static Integer[] arrTimeOut = { 0, 10, 15, 20, 30 };
	private static Integer[] arrMaxDeep = { 0, 1, 2, 3, 4 };
	private ArrayList<String> m_CityFileList = new ArrayList<String>();
	private ArrayList<String> m_CityNameList = new ArrayList<String>();
	private CheckBox m_objCheckApp = null;
	private CheckBox m_objCheckData = null;
	private Handler hander = null;
	private ProgressDialog m_dlgPro = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.config);
		m_objDataList = (Spinner) this.findViewById(R.id.dataListSpinner);
		m_objDataList.setEnabled(false);
		m_objTimeOut = (Spinner) this.findViewById(R.id.timeoutSpinner);
		m_objMaxDeep = (Spinner) this.findViewById(R.id.maxdeepSpinner);
		m_objMoreData = (Button) this.findViewById(R.id.moredata);
		m_objCheckApp = (CheckBox)  this.findViewById(R.id.CheckAppVersion);
		m_objCheckData = (CheckBox)  this.findViewById(R.id.CheckDataVersion);
		m_objCheckApp.setChecked(((CityBusApp) getApplicationContext()).m_objConfigMgr.getCheckApp());
		m_objCheckData.setChecked(((CityBusApp) getApplicationContext()).m_objConfigMgr.getCheckData());
		setListener();
		m_dlgPro = new ProgressDialog(ConfigActivity.this);
		m_dlgPro.setTitle("Waiting...");
		m_dlgPro.setMessage( "正在读取数据文件信息...");
		m_dlgPro.show();
		Context context = this.getApplicationContext();
		ArrayAdapter<Integer> adapter2 = new ArrayAdapter<Integer>(this,
				android.R.layout.simple_spinner_item, arrTimeOut);
		adapter2
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		m_objTimeOut.setAdapter(adapter2);
		ArrayAdapter<Integer> adapter3 = new ArrayAdapter<Integer>(this,
				android.R.layout.simple_spinner_item, arrMaxDeep);
		adapter3
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		m_objMaxDeep.setAdapter(adapter3);
		int intMaxDeep = ((CityBusApp) context).m_objConfigMgr.getMaxDeep();
		int intTimeOut = ((CityBusApp) context).m_objConfigMgr.getTimeOut();
		for (int i = 0; i < arrMaxDeep.length; i++) {
			if (arrMaxDeep[i].intValue() == intMaxDeep) {
				m_objMaxDeep.setSelection(i, true);
				break;
			}
		}
		for (int i = 0; i < arrTimeOut.length; i++) {
			if (arrTimeOut[i].intValue() == intTimeOut) {
				m_objTimeOut.setSelection(i, true);
				break;
			}
		}
		hander = new Handler() {
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 0:
					if(0== ((ArrayAdapter<String>)(msg.obj)).getCount()){
						m_objDataList.setEnabled(false);
						m_objMoreData.requestFocus();
						showInfo("本地没有任何城市的数据，请点击“更多数据”下载你所需要的数据！");
					}else{
						m_objDataList.setEnabled(true);
						m_objDataList.setAdapter((ArrayAdapter<String>)(msg.obj));
					}
					m_dlgPro.dismiss();
					break;
				case 1:
					m_dlgPro.setMessage( "正在读取数据文件信息,完成"+ String.valueOf(msg.arg1) +"%...");
					break;	
					}
			}
		};
		
		
		new Thread(){
			public void run(){
				fillDataList();
			}
		}.start();
	
	

	}

	protected void onPause() {
		super.onPause();
	}

	private void fillDataList() {
		Context context = this.getApplicationContext();
		String[] DataFile = context.fileList();
		
		m_CityFileList.clear();
		m_CityNameList.clear();
		if(((CityBusApp)getApplication()).m_objBusScan.isInited()){
			m_CityFileList.add(((CityBusApp)getApplication()).m_objBusScan.getFileName());
			m_CityNameList.add(((CityBusApp)getApplication()).m_objBusScan.DataName);
		}

		for(int i= 0 ;i<DataFile.length;i++){
			sendMsg(1,i*100/DataFile.length,0,null);
			if (DataFile[i].endsWith(".cbus")) {
				BusScan bs = new BusScan(context, DataFile[i]);
				boolean tt = bs.readHeader();
				if (tt==true) {
					m_CityFileList.add(DataFile[i]);
					m_CityNameList.add(bs.DataName);
					
				}
			}
			
		}
		String[] CityList = new String[m_CityNameList.size()];
		m_CityNameList.toArray(CityList);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, CityList);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// m_objDataList.setSelection(position)
		

		sendMsg(0,0,0,adapter);

		// m_objMaxDeep.setSelection(3,true);

	}

	private void setListener() {
		m_objCheckApp.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				((CityBusApp) getApplicationContext()).m_objConfigMgr.putBoolean("CheckVersionApp", isChecked);
				if(!isChecked)
					showInfo("关闭更新能将不能即时获取最新的软件版本，若发现问题后请手动开启更新或到官网重新下载");
				// TODO Auto-generated method stub
			}
		});
		m_objCheckData.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				((CityBusApp) getApplicationContext()).m_objConfigMgr.putBoolean("CheckVersionData", isChecked);
				
				if(!isChecked)
					showInfo("关闭数据更新能将不能即时获取最新的数据");
				// TODO Auto-generated method stub
			}
		});
		m_objDataList.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				((CityBusApp) getApplicationContext()).m_objConfigMgr
						.putDataFile(m_CityFileList.get(arg2));
				// Toast.makeText(ConfigActivity.this.getApplicationContext(),
				// "设置己经保存！", Toast.LENGTH_SHORT).show();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		m_objTimeOut.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				((CityBusApp) getApplicationContext()).m_objConfigMgr
						.putTimeOut(arrTimeOut[arg2].intValue());
				// Toast.makeText(ConfigActivity.this.getApplicationContext(),
				// "设置己经保存！", Toast.LENGTH_SHORT).show();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		m_objMaxDeep.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				((CityBusApp) getApplicationContext()).m_objConfigMgr
						.putMaxDeep(arrMaxDeep[arg2].intValue());
				// Toast.makeText(ConfigActivity.this.getApplicationContext(),
				// "设置己经保存！", Toast.LENGTH_SHORT).show();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		m_objMoreData.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getApplicationContext(),
						DownLoadDataFileActivity.class);
				startActivityForResult(intent, 1);
				

			}

		});

	}
	private void sendMsg(int flag,int arg1,int arg2,Object object) {
		Message msg = new Message();
		msg.arg1 = arg1;
		msg.arg2 = arg2;
		msg.what = flag;
		msg.obj = object;
		hander.sendMessage(msg);
	}
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// B返回时触发
		m_dlgPro.show();
		new Thread(){
			public void run(){
				fillDataList();
			}
		}.start();
	}
	protected void showInfo(String msg){
		Toast.makeText(ConfigActivity.this, msg, Toast.LENGTH_LONG).show();
	}
	public void onFlingLeft() {
		// TODO Auto-generated method stub
		this.finish();
 
	}
}
